var documentPanel;
var newid;
var documentArray;

var timeTest = /^([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])$/i;
Ext.apply(Ext.form.field.VTypes, {
    //  vtype validation function
    time: function(val, field) {
        return timeTest.test(val);
    },
    // vtype Text property: The error text to display when the validation function returns false
    timeText: 'Not a valid time.  Must be in the format "12:34:23".',
    // vtype Mask property: The keystroke filter mask
    timeMask: /[\d\s:amp]/i
});

function getDocumentArray() {
    return documentArray;
}
 var panel, form;

Ext.define('ContractEquipmentSchedule', {
    extend:'Ext.data.Model',
    fields:[
        {
            name:'id'
        },

        {
            name:'contractid'
        },

        {
            name:'equipmentid'
        },

        {
            name:'MonStart'
        },

        {
            name:'MonEnd'
        },

        {
            name:'TueStart'
        },

        {
            name:'TueEnd'
        },

        {
            name:'WedStart'
        },

        {
            name:'WedEnd'
        },
        {
            name:'ThuStart'
        },

        {
            name:'ThuEnd'
        },

        {
            name:'FriStart'
        },

        {
            name:'FriEnd'
        },

        {
            name:'SatStart'
        },
		{
            name:'SatEnd'
        },

        {
            name:'SunStart'
        },

        {
            name:'SunEnd'
        },

        {
            name:'lastupdate'
        }
    ]
});


Ext.onReady(function () {
    documentArray = new Array();
    var request;
    newid = 0;
});
function createPanel(equipmentid) {
        if (!panel) {

            form = Ext.create('Ext.form.Panel', {
                id:'equipschedule-form',
				// url: 'save-form.php',
                bodyPadding:10,
                defaults:{
                    anchor:'100%'
                },
                url:'index.php?r=ContractEquipSchedule/create&contractid='+contractid+'&equipmentid='+equipmentid,
                items:[
								{
							xtype:'fieldset',
							checkboxToggle:true,
							title: 'Auto Fill',
						//	defaultType: 'textfield',
							collapsed: false,
							layout: 'anchor',
							defaults: {
								anchor: '100%'
							},
							items :[
								{
									xtype: 'button',
									text : 'Business Hours',
									handler : function()
									{
										form.getForm().load({
										   url: 'index.php?r=ContractEquipSchedule/business',
										   method: 'GET',
										   waitMsg: 'Loading data...'
										});
									}
								},{
									xtype: 'button',
									text : 'Extended Hours',
									handler : function()
									{
										form.getForm().load({
										   url: 'index.php?r=ContractEquipSchedule/extended',
										   method: 'GET',
										   waitMsg: 'Loading data...'
										});
									}
								},
							]
						},
					
                    {
                        xtype:'textfield',
                        name:'MonStart',
                        fieldLabel:'Monday Start',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'MonEnd',
                        fieldLabel:'Monday End',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'TueStart',
                        fieldLabel:'Tuesday Start',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'TueEnd',
                        fieldLabel:'Tuesday End',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'WedStart',
                        fieldLabel:'Wednesday Start',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'WedEnd',
                        fieldLabel:'Wednesday End',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'ThuStart',
                        fieldLabel:'Thursday Start',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'ThuEnd',
                        fieldLabel:'Thursday End',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'FriStart',
                        fieldLabel:'Friday Start',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'FriEnd',
                        fieldLabel:'Friday End',
						size : 8,
						vtype: 'time',
                    },
					{
                        xtype:'textfield',
                        name:'SatStart',
                        fieldLabel:'Saturday Start',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'SatEnd',
                        fieldLabel:'Saturday End',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'SunStart',
                        fieldLabel:'Sunday Start',
						size : 8,
						vtype: 'time'
                    },
					{
                        xtype:'textfield',
                        name:'SunEnd',
                        fieldLabel:'Sunday End',
						vtype: 'time'
                    }
					
              
                ],
				buttons: [
				{
					text: 'Submit',
					iconCls : 'add-button',
					//formBind: true, //only enabled once the form is valid
					//disabled: true,
					handler: function() {
						var form = this.up('form').getForm();
						if (form.isValid()) {
							form.submit({
								success: function(form, action) {
								 // Ext.Msg.alert('Success', action.result.success);
								  panel.close();
								  partGrid.getView().refresh();
								  //alert('Sukses');
								},
								failure: function(form, action) {
								Ext.Msg.alert('Failed', action.result.msg);
								panel.close();
								//alert('gagal')
								}
							});
						}
					}
				},
				{
					text: 'Reset',
					handler: function() {
						this.up('form').getForm().reset();
					}
				}
				],
            });

            panel = Ext.create('Ext.window.Window', {
                title:'Equipment Schedule',
                width:300,
                height:550,
                layout:'fit',
                items:form,
                closeAction:'hide',
				autoScroll:true,
                // buttons:[
                    // {
                        // text:'Save',
						// iconCls : 'add-button',
						
                        // handler:function () {
                            // form.submit({
                                // url:function () {
                                    // if (request == 'create') {
                                        // //return 'index.php?r=contractDocuments/create&contractid=' + contractid;
										// alert(contractid+' '+equipmentid);
                                    // } else {
										// alert('update');
                                        // //return 'index.php?r=contractDocuments/update&id=' + form.getRecord().data.id;
                                    // }
                                // },
                                // success:function () {
                                    // grid.getStore().load();
                                    // panel.hide();
                                // }

                            // });
                        // }
                    // },
                    // {
                        // text:'Close',
                        // handler:function () {
                            // panel.hide();
                        // }
                    // }
                // ]
            });
        }
        panel.show();
        panel.center();
		
		form.getForm().load({
		   url: 'index.php?r=ContractEquipSchedule/business',
		   method: 'GET',
		   waitMsg: 'Loading data...'
		});
		
		form.getForm().load({
		   url: 'index.php?r=ContractEquipSchedule/check&contractid='+contractid+'&equipmentid='+equipmentid,
		   method: 'GET',
		   waitMsg: 'Loading data...'
		});
    }
function showForm(equipmentid)
	{
		
		 // request = 'create';
		 // $.ajax({
			// url : 'index.php?r=ContractEquipSchedule/check&contractid='+contractid+'&equipmentid='+equipmentid,
			// type: 'GET',
			// success : function(data)
			// {
				// form.getForm().loadRecord(data);
			// }
		 // })
		 
		// var myStore = Ext.create('Ext.data.Store', {
        // model: 'ContractEquipmentSchedule',
        // proxy: {
				// type: 'ajax',
				// url : 'index.php?r=ContractEquipSchedule/check&contractid='+contractid+'&equipmentid='+equipmentid,
				// reader: {
					// type: 'json'
				// }
			// },
			// autoLoad: true,
			// listeners: {
				// load: function (store,record,e) {
						// form.getForm().loadRecord(record[0]);
						// // form.getForm().loadRecord(records[0]);
						
				// }
			// }
		// });
		
		 
		createPanel(equipmentid);
			
	}